package com.ikun.hosuserservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ikun.common.entity.ResponseResult;
import com.ikun.common.entity.ResponseStatus;
import com.ikun.common.entity.User;
import com.ikun.hosuserservice.entity.UserData;
import com.ikun.hosuserservice.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 注册
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserOperateController {

    @Autowired
    private UserService userService;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    /**
     * 根据id找到用户
     * @param userId
     * @return
     */
    @GetMapping("getOneUserByUserId/{userId}")
    public ResponseResult getOneUser(@PathVariable String userId) {
        User userData = userService.getById(userId);
        return ResponseResult.ok(userData);
    }

    /**
     * 根据id更新用户
     * @param user
     * @return
     */
    @PostMapping("/updateUserByUserId")
    public ResponseResult updateUserByUserId(@RequestBody User user) {
        user.setUpdateTime(LocalDateTime.now());
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        System.out.println("修改后的user" + user);
        User userData = userService.getById(user);
        return ResponseResult.ok(userData);

    }
}


